package team.seekers.oj.group.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team.seekers.oj.common.entity.ApiResponseBody;
import team.seekers.oj.common.enums.ApiExceptionEnum;
import team.seekers.oj.common.exception.ApiException;
import team.seekers.oj.common.exception.InternalApiException;
import team.seekers.oj.group.api.GroupApi;
import team.seekers.oj.group.service.GroupExtensionService;
import team.seekers.oj.group.service.GroupService;
import team.seekers.oj.user.api.UserApi;
import team.seekers.oj.user.dto.UserDTO;
import team.seekers.oj.user.service.UserExtensionService;
import team.seekers.oj.user.service.UserService;

import java.util.List;
import java.util.Map;

/**
 * @ClassName GroupInternalController
 * @Description TODO
 * @Author hbc
 * @Date 2022/1/20
 * @Version V1.0
 **/

@RestController
public class GroupInternalController implements GroupApi {

    @Autowired
    private GroupService groupService;

    @Autowired
    private GroupExtensionService groupExtensionService;


    @Override
    public List<Long> groupIdToUserIds(Long groupId) {
        return groupService.queryIdToUserIds(groupId);
    }


    @Override
    public void addGroupParticipateContest(long groupId, long contestId) {
        // TODO: 外置的自动 retry 机制
        for (int i = 0; i < 5; i++) {
            try {
                groupExtensionService.addGroupParticipateContest(groupId, contestId);
            } catch (ApiException e) {
                if (ApiExceptionEnum.SERVER_BUSY.code == e.code) {
                    continue;
                }
            }
            break;
        }
    }

    @Override
    public void addGroupParticipateHomework(long groupId, long homeworkId) {
        // TODO: 外置的自动 retry 机制
        for (int i = 0; i < 5; i++) {
            try {
                groupExtensionService.addGroupParticipateHomework(groupId, homeworkId);
            } catch (ApiException e) {
                if (ApiExceptionEnum.SERVER_BUSY.code == e.code) {
                    continue;
                }
            }
            break;
        }
    }

    @Override
    public void addGroupParticipateCourse(long groupId, long courseId) {
        // TODO: 外置的自动 retry 机制
        for (int i = 0; i < 5; i++) {
            try {
                groupExtensionService.addGroupParticipateCourse(groupId, courseId);
            } catch (ApiException e) {
                if (ApiExceptionEnum.SERVER_BUSY.code == e.code) {
                    continue;
                }
            }
            break;
        }
    }




    @Override
    public List<Long> queryParticipateCourse( long groupId) {
        return groupExtensionService.queryParticipateCourse(groupId);
    }

    @Override
    public List<Long> queryParticipateContest( long groupId) {
        return groupExtensionService.queryParticipateContest(groupId);
    }

    @Override
    public List<Long> queryParticipateHomework( long groupId) {
        return groupExtensionService.queryParticipateHomework(groupId);
    }
}
